#include "NetworkException.h"

int net::NetworkException::NO_ERROR_CODE = 0;

net::NetworkException::NetworkException(string message)
{
	errorCode = NO_ERROR_CODE;
	customMessage = message;
}

net::NetworkException::NetworkException(int errorCode)
{
	this->errorCode = errorCode;
	customMessage = "";
}

net::NetworkException::~NetworkException(void)
{
}

string net::NetworkException::getMessage(void)
{
	string errorMessage;
	if (errorCode > NO_ERROR_CODE)
	{
		errorMessage = getErrorMessage(this->errorCode);
	}
	else
	{
		errorMessage = customMessage;
	}
	return errorMessage;
}

int net::NetworkException::getErrorCode(void)
{
	return errorCode;
}

string net::NetworkException::getErrorMessage(int errorCode)
{
	LPSTR errString = NULL;
    int size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 
		0, errorCode, 0, (LPWSTR)&errString, 0, 0);
     
     stringstream ss;
     ss << errString;
     LocalFree(errString);
     return ss.str();
}
